//
//  BFHotAppViewController.m
//  13KModel
//
//  Created by ISS on 25/09/12.
//
//
// CLASS INCLUDES
#import "PubInc.h"
#import "YouMiWall.h"
#import "BFHotAppViewController.h"

// CONST DEFINE
NSInteger const kOnePageAppCount = 30;

@interface BFHotAppViewController ()

- (void)initYouMiWall;

@end

@implementation BFHotAppViewController
@synthesize tableView = _tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    
    [self initYouMiWall];
    [self initNavBar];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)initYouMiWall
{
    openApps = [[NSMutableArray alloc] init];
    wall = [[YouMiWall alloc] initWithAppID:kGlobalYouMiAdAppID
                              withAppSecret:kGlobalYouMiAdAppSecert];
    wall.delegate = self;
    
    [wall requestOffersAppData:NO pageCount:kOnePageAppCount];
    
    [self startLoadAnimationWithText:NSLocalizedString(@"加载数据", nil)];
}

- (void)initNavBar
{
    // 返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(navigateToPrevView) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0,
                                button.imageView.image.size.width,
                                button.imageView.image.size.height)];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = backButton;
    self.title = @"精品推荐";
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [openApps count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Noe Rewarded Cell Identifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    if (indexPath.row >= [openApps count]) return cell;
    
    YouMiWallAppModel *model = [openApps objectAtIndex:indexPath.row];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.desc;
    cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.smallIconURL]]];
    
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row >= [openApps count]) return;
    
    YouMiWallAppModel *model = [openApps objectAtIndex:indexPath.row];
    [wall userInstallOffersApp:model];
}

#pragma mark - YouMiWall delegate

- (void)didReceiveOffersAppData:(YouMiWall *)adWall offersApp:(NSArray *)apps
{
    [openApps addObjectsFromArray:apps];
    [[self tableView] reloadData];

    [self stopLoadAnimation];
}

- (void)didFailToReceiveOffersAppData:(YouMiWall *)adWall error:(NSError *)error
{
    [self stopLoadAnimation];
    [self showAlertMessageBox:NSLocalizedString(@"亲, 网络不给力啊!", nil)];
}

- (void)didReceiveOffers:(YouMiWall *)adWall
{
}

- (void)didFailToReceiveOffers:(YouMiWall *)adWall error:(NSError *)error
{
}

@end
